package njci.softerware.com.myexpress2017.serviceObj.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher;
import njci.softerware.com.myexpress2017.serviceObj.PackageSign;

public class PackageDispatcherDao {
    private Context context;
    private Dao<PackageDispatcher, Integer> disPatcherDao;
    private DatabaseHelper helper;

    public PackageDispatcherDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            disPatcherDao = helper.getDao(PackageDispatcher.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param packageDispatcher
     * @throws SQLException
     */
    public void add(njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher packageDispatcher) {
        try {
            disPatcherDao.create(packageDispatcher);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher get(int id) {
        try {
            return disPatcherDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delteExpressUser(njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher user) {
        try {
            disPatcherDao.delete(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void updateExpressUser(njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher user) {
        try {
            disPatcherDao.update(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher> findAllUser() {
        List<njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher> list = null;
        try {
            list = disPatcherDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher> getListByUserId(Integer user_id) {
        List<njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher> listUser = null;
        try {
            listUser = disPatcherDao.queryBuilder().where().eq("user_id", user_id).query();
            if (listUser != null && listUser.size() > 0.) {
                return listUser;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listUser;
    }


    public njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher getByUserIdAndDate(Integer user_id, String dispatcher_date) {
        List<njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher> listUser = null;
        PackageDispatcher p = null;
        try {
            listUser = disPatcherDao.queryBuilder().where().eq("user_id", user_id).and().like("dispatcher_date", dispatcher_date).query();
            if (listUser != null && listUser.size() > 0.) {
                p = listUser.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return p;
    }

    public void addList(List<njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher> list) {
        Iterator<njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher> it = list.iterator();
        while (it.hasNext()) {
            njci.softerware.com.myexpress2017.serviceObj.PackageDispatcher packageDispatcher = it.next();
            PackageDispatcher pold;
            if ((pold =getByUserIdAndDate(packageDispatcher.getUser_id(), packageDispatcher.getDispatcher_date())) == null && packageDispatcher.getCount_dispatch() > 0) {
                this.add(packageDispatcher);
            } else if (pold  != null && pold.getCount_dispatch() != packageDispatcher.getCount_dispatch() && packageDispatcher.getCount_dispatch() > 0) {
                pold.setCount_dispatch(packageDispatcher.getCount_dispatch());
                this.updateExpressUser(packageDispatcher);
            }
        }
    }

    public Integer getListCount(int user_Id, String dataArg, List<String> weekList) {
        int count = 0;
        try {

            QueryBuilder builder = disPatcherDao.queryBuilder();
            List<PackageDispatcher> listUser = null;
            if (weekList != null && weekList.size() > 0) {
                listUser = builder.where().eq("user_id", user_Id).and().eq("dispatcher_date", weekList.get(0)).or().eq("dispatcher_date", weekList.get(1)).or().eq("dispatcher_date", weekList.get(2)).or().
                        eq("dispatcher_date", weekList.get(3)).or().eq("dispatcher_date", weekList.get(4)).or().eq("dispatcher_date", weekList.get(5)).or().eq("dispatcher_date", weekList.get(6)).query();
            } else {
                listUser = builder.where().eq("user_id", user_Id).and().like("dispatcher_date", dataArg).query();
            }
            if (listUser != null && listUser.size() > 0) {
                Iterator<PackageDispatcher> it = listUser.iterator();
                while (it.hasNext()) {
                    PackageDispatcher p = it.next();
                    count = count + p.getCount_dispatch();
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public List<PackageDispatcher> getListCountBySelection(int user_Id, String dataArg, List<String> weekList) {
        List<PackageDispatcher> listUser = null;
        try {

            QueryBuilder builder = disPatcherDao.queryBuilder();
            if (weekList != null && weekList.size() > 0) {
                listUser = builder.where().eq("user_id", user_Id).and().eq("dispatcher_date", weekList.get(0)).or().eq("dispatcher_date", weekList.get(1)).or().eq("dispatcher_date", weekList.get(2)).or().
                        eq("dispatcher_date", weekList.get(3)).or().eq("dispatcher_date", weekList.get(4)).or().eq("dispatcher_date", weekList.get(5)).or().eq("dispatcher_date", weekList.get(6)).query();

            } else {
                listUser = builder.where().eq("user_id", user_Id).and().like("dispatcher_date", dataArg).query();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listUser;
    }
}
